package com.starblink.android.basic.widget.gloading

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.starblink.android.basic.databinding.ViewSkGloadingGlobalStatusBinding
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visible

class SkLoadingGlobalStatusView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0,
    retryTask: Runnable? = null,
) : FrameLayout(context, attrs, defStyleRes) {


    private val binding = ViewSkGloadingGlobalStatusBinding.inflate(this.inflater, this)

    init {

        retryTask?.let {
            binding.empty.onBtnClick = {
                it.run()
            }
        }
        binding.empty.hideText1 = false
        binding.empty.hideText2 = false
    }


    fun setStatus(status: Int, msg: String?) {
        var show = true //是否展示这个布局
        when (status) {
            Gloading.STATUS_LOAD_SUCCESS -> {
                binding.loadingView.gone()
                binding.loadingAnim.pauseAnimation()
                binding.empty.gone()
                show = false
            }
            Gloading.STATUS_NORMAL -> {
                binding.loadingView.visible()
                binding.loadingAnim.playAnimation()
                binding.empty.gone()
            }
            Gloading.STATUS_LOADING -> {
                binding.loadingView.visible()
                binding.loadingAnim.playAnimation()
                binding.empty.gone()
            }
            Gloading.STATUS_LOAD_FAILED -> {
                binding.loadingView.gone()
                binding.loadingAnim.pauseAnimation()
                binding.empty.visible()
            }
            Gloading.STATUS_EMPTY_DATA -> {
                binding.loadingView.gone()
                binding.loadingAnim.pauseAnimation()
                binding.empty.visible()
                binding.empty.text2 = msg
            }
            else -> {}
        }
        visibility = if (show) View.VISIBLE else View.GONE
    }

}